package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.service.*;
import com.github.pagehelper.PageInfo;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

import cn.wolfcode.trip.base.domain.Strategy;

import cn.wolfcode.trip.base.query.StrategyQueryObject;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.apache.ibatis.ognl.DynamicSubscript.all;

@Controller
@RequestMapping("/hotelDetail")
public class HotelDetailContorller {
    @Autowired
    private IHotelDetailService hotelDetailService;
    @Autowired
    private ServletContext ctx;
    @Autowired
    private IHotelLikeService hotelLikeService;
    @Autowired
    private IHotelViewsService hotelViewsService;



    @PostMapping("/getHotelandInsertClient")
    @ResponseBody
    public Object getHotelMessage(String city,HotelClientMessage hotelClientMessage){
        List<HotelDetail> h= hotelDetailService.insertClientandHotelRegionName(city,hotelClientMessage);
        return h;
    }
    //推荐的酒店
    @GetMapping("/{state}/stateHotel")
    @ResponseBody
    public List<HotelDetail> getStateHotel(@PathVariable int state){
        List<HotelDetail> h = hotelDetailService.getHotelDetailByHotelRecommend(state);
        return h;
    }

    //查询出所有的酒店的点赞总数
    @GetMapping("/{id}/like")
    @ResponseBody
    public Object getLike(@PathVariable Long id){

        return hotelDetailService.getLikeSum(id);
    }

    @GetMapping(value = "/{id}",produces = "text/html;charset=utf-8")
    public void hot(@PathVariable Long id, HttpServletResponse resp) throws Exception {
        List<HotelDetail> hotelDetail = hotelDetailService.getHotelDetails(id);
        //----------------------------------
        //创建配置对象,设置版本号就不会过时
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //指定模板的文件位置
        //要用servletContext 这个容器才有getRealPath
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/templates")));
        //获取模板,并解决乱码问题
        Template temp = cfg.getTemplate("hotelPage.ftl","UTF-8");
        //合并:数据+模板
        //freemark 只能接受一个数据,使用map打包,因为时pageInfo,所以要变量.list
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("hotelDetails",hotelDetail);
        //输出到页面,类似于modal
        temp.process(map,resp.getWriter());
    }

    //酒店点赞
     @PostMapping("/likeCount")
    @ResponseBody
    public Object getHotelDetails(HotelLike hotelLike){
        //查询是否有该用户的点赞记录
         HotelLike likeRecord1 = hotelLikeService.getLikeRecord(hotelLike);
         int result = 0;
         if (likeRecord1 != null) {
             //如果找到这条记录,删除这条记录,已点赞
             hotelLikeService.deleteRecord(likeRecord1.getId());
             //如果找到这条记录，删除该记录，同时文章的点赞数减一
             //根据点赞表酒店id找到酒店
             HotelDetail hotelDetail = hotelDetailService.selectHotel(likeRecord1.getHotelid());
             //文章点赞数减一
             hotelDetail.setLikecount(hotelDetail.getLikecount()-1);
             result = hotelDetail.getLikecount();
             //更新酒店的点赞数
             hotelDetailService.updateHotelLikeRecord(hotelDetail);
         }else {
             //如果没有返回值,说明没有点赞
             //保存点赞
             hotelLike.setLiketime(new Date());//添加更新时间
             hotelLikeService.insertRecord(hotelLike);//保存点赞记录
             HotelDetail hotelDetail = hotelDetailService.selectHotel(hotelLike.getHotelid());//根据点赞记录用户id查询 酒店记录
             Integer likecount = hotelDetail.getLikecount();
             //点赞数+1
             hotelDetail.setLikecount(likecount+1);//将酒店的点赞记录+1
             result = hotelDetail.getLikecount();//设置到上面的变量中
             hotelDetailService.updateHotelLikeRecord(hotelDetail);//更新点赞数
         }
         return result;
    }

    //浏览量统计
    //查询出所有的酒店的点赞总数
    @GetMapping("/pageview")
    @ResponseBody
    public Object getPageview(HotelViews hotelViews){
        //查询出该用户是否浏览过
        HotelViews viewsRecord = hotelViewsService.getViewsRecord(hotelViews);
        int result = 0;
        //如果浏览记录不等于null说明该用户浏览过了,不需要增加浏览量
        if (viewsRecord != null) {
            //查询酒店现在的浏览量即可
            int pageView = hotelDetailService.getPageView(viewsRecord.getHid());
            result = pageView;
        }else {
            //如果没有记录说明没有浏览过,添加浏览记录
            hotelViews.setBrowsetiem(new Date());
            hotelViewsService.insertPageview(hotelViews);
            //获取浏览量和id
            HotelDetail pageViewAndId = hotelDetailService.getPageViewAndId(hotelViews.getHid());
            //设置浏览量+1
            pageViewAndId.setPageview(pageViewAndId.getPageview()+1);
            result = pageViewAndId.getPageview();
            //更新数据库的浏览量
            hotelDetailService.updatePageview(pageViewAndId);

        }
        return result;
    }
}
